﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 新门禁.Utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
using static 新门禁.Utils.HikLprSession;

namespace 新门禁
{
    /// <summary>
    /// HandPassWindow.xaml 的交互逻辑
    /// </summary>
    public partial class HandPassWindow : Window
    {
        string inOutType = "";
        string carNo = "";
      
        public HandPassWindow()
        {
            InitializeComponent();
        }

        public void setInOutType(String type)
        {
            this.inOutType = type;
        }
        public void setCarNo(String carNo)
        {
            this.carNo = carNo;
        }
        public string getCarNo()
        {
            return carNo;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            carNo= txtCarNumber.Text.Trim();
            if (txtMemo.Text.Trim().Length < 2)
            {
                MessageBox.Show("请输入手动放行原因");
                return;
            }
            if (txtCarNumber.Text.Trim().Length < 2)
            {
                MessageBox.Show("请输入正确车牌号");
                return;
            }

            if (inOutType.Equals("in"))
            {

                try
                {

                    String parkCode = App.GetBaseInfo().parkCode;
                    String ip = App.GetBaseInfo().HttpServerIp;
                    String port = App.GetBaseInfo().HttpServerPort;
                    MainWindow._camIn?.BarrierGateCtrl(BarrierCmd.Open, channel: 1, laneNo: 1, entranceNo: 1);//抬杆
                    JObject body = new JObject();
                    body.Add("msg", txtMemo.Text.Trim());//放行原因
                    body.Add("plateNo", txtCarNumber.Text.Trim());
                    body.Add("direct", "0");
                    body.Add("parkCode", parkCode);
                    String result = HttpPostHelper.doHttpPost("http://" + ip + ":" + port + "/qd-s-factory/kafka/pushCar/", body.ToString());
                    this.DialogResult = true;

                    this.Close();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
            else
            {

                try
                {

                    String parkCode = App.GetBaseInfo().parkCode;
                    String ip = App.GetBaseInfo().HttpServerIp;
                    String port = App.GetBaseInfo().HttpServerPort;
                    //JObject body = new JObject();
                    //body.Add("msg", txtMemo.Text.Trim());//放行原因
                    //body.Add("plateNo", txtCarNumber.Text.Trim());
                    //body.Add("direct", "0");
                    //body.Add("parkCode", parkCode);
                    //String result = HttpPostHelper.doHttpPost("http://" + ip + ":" + port + "/qd-s-factory/kafka/pushCar/", body.ToString());
                    //MainWindow._camIn?.BarrierGateCtrl(BarrierCmd.Open, channel: 1, laneNo: 1, entranceNo: 1);//抬杆

                    var param = new Dictionary<string, string>();

                    param["useStatus"] = "0";
                    param["payStatus"] = "1";
                    param["carNumber"] = txtCarNumber.Text.Trim();  // plate 变量
                    // 3) 发送
                    String result = HttpPostHelper.doHttpGet("http://" + ip + ":" + port + "/qd-s-consumerapp/wxPaymentLog/detailOne", param);//改变车辆进场状态
                    JObject resJSON = JObject.Parse(result);
                    string status = (string)resJSON.GetValue("code");
                    string message = (string)resJSON.GetValue("message");
                    if ("00000".Equals(status))
                    {
                        JToken data = resJSON.GetValue("data");
                        if (data != null && data.Type == JTokenType.Object && data.HasValues)
                        {


                            MainWindow._camOut?.BarrierGateCtrl(BarrierCmd.Open, channel: 1, laneNo: 1, entranceNo: 1);//抬杆

                            JObject body2 = new JObject();
                            body2.Add("plateNo", txtCarNumber.Text.Trim());
                            body2.Add("direct", "1");
                            body2.Add("parkCode", parkCode);
                            body2.Add("msg", txtMemo.Text.Trim());//放行原因
                            // 3) 发送
                            String result2 = HttpPostHelper.doHttpPost("http://" + ip + ":" + port + "/qd-s-factory/kafka/pushCar", body2.ToString());
                            this.DialogResult = true;
                            this.Close();


                        }
                        else
                        {
                            
                            this.DialogResult = false;
                            this.Close();
                            //  MessageBox.Show(message);
                        }


                    }
                    else
                    {
                       
                        MessageBox.Show(message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }


            }
            //服务器验证是否通行

        }

        private void Button_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
